package Servlet;

import model.User;
import Utils.DBUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取到表单数据
        request.setCharacterEncoding("utf-8");
        String Name = request.getParameter("Name");
        String password = request.getParameter("password");
        User user=new User(Name,password);
        //判断是否登录成功
        DBUtils db = null;
        try {
            db = new DBUtils();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        User User1 = db.checkedLogin("user",user);
		if (User1.getLevel()==1) {
			//属性设置，为跳转到目标页面，做数据准备【如有必要，还需要进一步查询数据库】
			//请求分派： request设置属性；  重定向：session设置属性或者url?参数1=参数值1&参数2=参数值1
            //数据准备工作
//            user = new User(,Name,password,1);
//            request.setAttribute("user",user);
            request.getSession().setAttribute("user",User1);
			System.out.println("管理员登录成功");
            RequestDispatcher rd = request.getRequestDispatcher("admin.jsp");
            rd.forward(request,response);
        }else if (User1.getLevel()==2){
            //属性设置，为跳转到目标页面，做数据准备【如有必要，还需要进一步查询数据库】
            //请求分派： request设置属性；  重定向：session设置属性或者url?参数1=参数值1&参数2=参数值1
            //数据准备工作
//             user = new User(Name,password,2);
//            request.setAttribute("user",user);
            request.getSession().setAttribute("user",User1);
            System.out.println("用户登录成功");
            RequestDispatcher rd = request.getRequestDispatcher("user.jsp");
            rd.forward(request,response);
        }

        else{
		    // 重定向：session设置属性或者url?参数1=参数值1&参数2=参数值1
            request.getSession().setAttribute("msg","111");
		    response.sendRedirect("error.jsp?id=888");
			System.out.println("登录失败");
		}
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
